package com.ltxtiyu.video.config;

import com.ltxtiyu.video.common.ConfigConstant;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbit相关配置
 */
@Configuration
public class RabbitConfig {
    /**
     * 消费者数量 默认为10
     */
    public static final int DEFAULT_CONCURRENT = 10;

    /**
     * 每个消费者获取最多投递消息数量, 最多50个
     */
    public static final int DEFAULT_PREFETCH_COUNT = 50;

    @Bean
    public Queue appPushMessage(){
        return new Queue(ConfigConstant.MESSAGE_NAME_APP_PUSH_MEASSGE);
    }
    @Bean
    public Queue momentRefreshWriteMessage(){
        return new Queue(ConfigConstant.MESSAGE_NAME_MOMENT_REFRESH_WRITE);
    }
    @Bean
    public Queue momentAddPlayTimesMessage(){
        return new Queue(ConfigConstant.MESSAGE_NAME_MOMENT_ADD_PLAY_TIMES);
    }

    /**
     * 并发消费配置
     * 用于消费者的方法,当前消费者执行完毕所有任务后才会去执行下一个消息
     * @param configurer
     * @param connectionFactory
     * @return
     */
    @Bean(name = "pointTaskContainerFactory")
    public SimpleRabbitListenerContainerFactory pointTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(DEFAULT_PREFETCH_COUNT);
        factory.setConcurrentConsumers(DEFAULT_CONCURRENT);
        configurer.configure(factory,connectionFactory);
        return factory;
    }
}
